import AlbumService from './album.service.js';
export default {
  name: "albums",
  data() {
    return {
      fileList: [],
      pageIndex: 1,
      pageSize: 10,
      addFileList: [],
      defaultPhoto: '',
      photoShow: false,
      isUser: false,
      // hostName: location.protocol + location.host + location.pathname
    };
  },
  methods: {
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleRemove(file, fileList) {
      fileList.splice(fileList.indexOf(file), 1);
    },
    handlePreview(file) {
      // console.log(file);
    },
    fileOver(files, fileList) {
      this.$message({
        type: 'warning',
        message: '每次最多只能上传10张图片!'
      });
    },
    checkImg(file) {
      if(file.size > 102400) {
        this.$message({
          type: 'warning',
          message: '哎呀图片太大了，换一张试试吧!'
        });
        return false;
      }else {
        return true;
      }
    },
    addFile(file) {
      let _self = this;
      let fileReader = new FileReader();  //本地预览
      fileReader.readAsDataURL(file.file);    //Base64
      fileReader.onload = function () {
        let img = new Image();
        img.src = fileReader.result;
        img.onload = function() {
          if(img.fileSize > 0 || (img.width > 0 && img.height > 0)) {
            let list = {
              name: file.file.name,
              uid: file.file.uid,
              base64: fileReader.result,
              extName: file.file.name.substring(file.file.name.length - 4, file.file.name.length),
              width: img.width,
              height: img.height
            };
            AlbumService.addPicture(list)
            .then((res) => {
              _self.searchAlbum();
            })
          }else {
            _self.$refs.upload.clearFiles();
            _self.$message({
              type: 'warning',
              message: '图片存在问题，请重新尝试上传!'
            });
          }
        }
      }
    },
    searchAlbum() {
      AlbumService.search(this.pageIndex, this.pageSize)
        .then(res => {
          let _src;
          location.origin === 'http://localhost:8080'? _src = '//localhost:80/': _src = '//www.miao-wu.cn/';
          console.log(location.origin);
          this.fileList = [];
          // res.data.list.map(i => {
          //   i.reUrl = this.hostName + i.reUrl;
          //   i.url = this.hostName + i.url;
          // });
          this.addFileList = res.data.list;
          for(let i = 0; i < this.addFileList.length; i++) {
            this.addFileList[i].reUrl = _src + this.addFileList[i].reUrl;
            this.addFileList[i].url = _src + this.addFileList[i].url;
          }
          this.defaultPhoto = _src + this.addFileList[0].url;
        })
        .catch(err => { throw (err); })
    },
    showPhoto(u) {
      this.defaultPhoto = u;
      this.photoShow = true;
    },
    closeShowPhoto() {
      this.photoShow = false;
    }
  },
  created() {
    // if(location.port === "8080") this.hostName = location.protocol + '//localhost:80' + location.pathname;
    this.searchAlbum();
    if(localStorage.getItem('HOMEUSERNAME')) {
      this.isUser = true;
    }else {
      this.isUser = false;
    }
  },
}
